extends IEffectBehavior


class_name HitScreen
"""
击中边界时,从屏幕另一侧出现
"""


enum ScreenMode {
    CROSS,
    REFECT,
}


## 触发的模式
@export var mode: ScreenMode = ScreenMode.CROSS


func support() -> Array:
    return [CharacterComponent.Event.ON_HIT_OUT_OF_SCREEN]

## 处理武器事件,返回是否取消销毁子弹
func do_event(character: CharacterComponent, v: EffectValue, e: CharacterEvent) -> void:
    if mode == ScreenMode.CROSS:
        cross_screen(character, v, e)
    elif mode == ScreenMode.REFECT:
        refect_screen(character, v, e)

func cross_screen(character: CharacterComponent, v: EffectValue, e: CharacterEvent) -> void:
    var screen_center: Vector2 = e.meta.projectile.get_viewport().get_visible_rect().size / 2
    var refect_angle: float = e.meta.direction.rotated(-PI / 2).angle()
    var cross_point: Vector2 = ShapeDraw.get_rect_center_intersect_point(screen_center, refect_angle)
    e.meta.projectile.global_position = cross_point + screen_center
    e.cancel()

func refect_screen(character: CharacterComponent, v: EffectValue, e: CharacterEvent) -> void:
    var screen_center: Vector2 = e.meta.projectile.get_viewport().get_visible_rect().size / 2
    var refect_angle: float = e.meta.direction.rotated(-PI / 2).angle()
    var cross_data: Dictionary = ShapeDraw.get_rect_center_intersect_line(screen_center, refect_angle)
    var move: MoveComponent = e.meta.projectile.element.subassembly("Move")
    if move:
        move.turn(move.direction.bounce(cross_data.normal))
        e.cancel()
